﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PricePortalLib;

namespace SWT_Handin_1
{
    public class FindItem : IFindItem
    {

        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello there! You should write some unit tests!");
        }

        private ISearchSubSupplier searchSubSupplier;
        private IItemCache itemCache;
        private uint cacheExpireTime;

        public FindItem(ISearchSubSupplier searchSubSupplier, IItemCache itemCache, uint cacheExpireTime)
        {
            this.searchSubSupplier = searchSubSupplier;
            this.itemCache = itemCache;
            SetCacheExpireTime(cacheExpireTime);
        }

        public List<IItem> LookUpItem(ulong partnumber, string description)
        {           
            List<IItem> result = new List<IItem>();
            DateTime expireDate = DateTime.Now.AddHours(-cacheExpireTime); //DateTime object containing the expire time

            //Search in our locally cached results
            List<IItem> itemCacheResults = itemCache.LookUpItem(partnumber, description);

            //check if the item has expired
            List<IItem> toOldItems = new List<IItem>();

            foreach(IItem cacheItem in itemCacheResults){
                int compare = cacheItem.Hit.CompareTo(expireDate);
                if (compare == -1 || compare == 0)
                {
                    toOldItems.Add(cacheItem);
                }
                else
                {
                    result.Add(cacheItem);
                }
            }
            itemCache.RemoveItems(toOldItems); //remove the old items from the local cache

            //if anything is in our results, local lookup succeded -> return the results
            if (result.Count > 0) return result;

            //no results found locally, look up 
            result = searchSubSupplier.LookUpItem(partnumber, description);

            //set hit for each item before adding them to the the local cache
            foreach(IItem item in result)
            {
                item.Hit = DateTime.Now;
            }
            itemCache.AddItems(result);

            //return whatever results we have
            return result;
        }

        public void SetCacheExpireTime(uint hours)
        {
            cacheExpireTime = hours;
        }
    }

    public class Item : IItem
    {

        private uint subSupplier;

        public uint SubSupplier
        {
            get
            {
                return subSupplier;
            }
            set
            {
                subSupplier = value;
            }
        }

        private ulong partNumber;

        public ulong PartNumber
        {
            get
            {
                return partNumber;
            }
            set
            {
                partNumber = value;
            }
        }

        private string description;

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        private DateTime hit;

        public DateTime Hit
        {
            get
            {
                return hit;
            }
            set
            {
                hit = value;
            }
        }
    }
}
